﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MQC.record
{
    public partial class AddPersonNo : System.Web.UI.Page
    {
        static string batch,department;
        protected void Page_Load(object sender, EventArgs e)
        {
            batch = Request.QueryString["batch"].ToString();
            department = Request.QueryString["department"].ToString();
        }

        protected void Btn_Click(object sender, EventArgs e)
        {
            using(DbAppDataContext db = new DbAppDataContext())
            {
                string[] batchs = batch.Split('|');
                bool run=true;
                string perosnNo="";
                string bt = "";
                foreach(var n in batchs)
                {
                    perosnNo = TxtNo.Text.Trim().ToString();
                    bt = n.ToString();
                    run = IsNoRepeat(perosnNo, n,department);
                    if(run)
                    {
                        Person_Log data = new Person_Log();
                        data.Batch = n;
                        data.PersonNo = perosnNo;
                        data.Department = department;
                        db.Person_Log.InsertOnSubmit(data);
                        db.SubmitChanges();
                        string s = string.Format("<script>alert('批次{0}添加成功');</script>", n);
                        Response.Write(s);
                    }
                    else
                    {
                        string s = string.Format("<script>alert('工号{0}在批次{1}已存在');</script>", perosnNo, n);
                        Response.Write(s);
                    }
                }

            }
        }
        public bool IsNoRepeat(string no, string batch,string department)
        {
            bool result = true;
            using (DbAppDataContext db = new DbAppDataContext())
            {
                var data = from m in db.Person_Log
                           where m.Batch == batch && m.PersonNo == no && m.Department==department
                           select m;
                if(data.ToList().Count!=0)
                {
                    result = false;
                }
            }
            return result;
        }
    }
}